﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web;

namespace up6.utils
{
    public class WebBase
    {
        /// <summary>
        /// 默认加载data/config/config.json
        /// </summary>
        public JToken m_config;
        public JObject m_site;
        //
        public JToken m_cfgObj;
        public ConfigReader m_webCfg;
        /// <summary>
        /// 页面级的变量
        /// 后台：
        ///     param["id"] = 1;
        /// 定义：
        ///     param.query 查询变量
        /// 前台：
        /// <%= this.param["id"]%>
        /// </summary>
        public JObject param = new JObject();

        public WebBase()
        {
            this.m_webCfg = new ConfigReader();
            this.m_cfgObj = this.m_webCfg.m_files;
        }



        /// <summary>
        /// 特殊字符替换
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        public string reqStringSafe(string name)
        {
            var v = name;
            v = v.Replace("\r", string.Empty);
            v = v.Replace("\n", string.Empty);
            v = v.Replace("'", string.Empty);
            v = v.Replace("\"", string.Empty);
            v = v.Replace(",", string.Empty);
            v = v.Replace(".", string.Empty);
            v = v.Replace("&", string.Empty);
            v = v.Replace(";", string.Empty);
            v = v.Replace("$", string.Empty);
            v = v.Replace("%", string.Empty);
            v = v.Replace("@", string.Empty);
            v = v.Replace(" ", string.Empty);
            v = v.Replace("()", string.Empty);
            v = v.Replace("+", string.Empty);
            v = v.Replace("script", string.Empty);
            v = v.Replace("document", string.Empty);
            v = v.Replace("eval", string.Empty);
            return v;
        }

        /// <summary>
        /// 从head域中获取base64字符串并自动解码
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public string headBase64(string n) { 
            byte[] outputb = Convert.FromBase64String(n);
            string os = Encoding.UTF8.GetString(outputb);
            return os;
        }

        /// <summary>
        /// 从form中获取base64字符串并自动解码
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public string formBase64(string n)
        {
            byte[] outputb = Convert.FromBase64String(n);
            string os = Encoding.UTF8.GetString(outputb);
            return os;
        }

       

        /// <summary>
        /// 向页面输出一个page变量，
        /// page.query
        /// page.url
        /// page.path
        /// </summary>
        /// <returns></returns>
        public string paramPage()
        {
            string v = string.Format("<script>var page={0};</script>", JsonConvert.SerializeObject(this.param));
            return v;
        }

        public string toInclude(string file)
        {
            bool css = file.ToLower().EndsWith("css");
            if (css)
            {
                return string.Format("<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\" />"
                    , file);
            }
            else
            {
                return string.Format("<script type=\"text/javascript\" src=\"{0}\" charset=\"{1}\"></script>"
                    , file
                    , "utf-8");
            }
        }

        public string require(params object[] ps)
        {
            StringBuilder sb = new StringBuilder();
            foreach (object f in ps)
            {
                //字符串
                if (Type.GetType("System.String") == f.GetType())
                {
                    sb.Append( toInclude(f.ToString()));
                }//json object
                else {
                    var t = JToken.FromObject(f);
                    //数组
                    if (t.Type == JTokenType.Array)
                    {
                        var arr = JArray.FromObject(t);
                        foreach (var a in arr)
                        {
                            sb.Append(toInclude(a.ToString()));
                        }
                    }
                    else {
                        sb.Append(toInclude(t.ToString()));
                    }
                }
            }
            return sb.ToString();
        }

       

        public string localFile(JToken t)
        {
            StringBuilder sb = new StringBuilder();

            if (t.Type == JTokenType.Array)
            {
                foreach (var jt in t)
                {
                    var data = this.localFile(jt.ToString());
                    sb.Append(data);
                }
            }
            else if (t.Type == JTokenType.String)
            {
                sb.Append(this.localFile(t.ToString()));
            }
            return sb.ToString();
        }

    }
}